package com.shengqi.xinnongren.work.ui.user.presenter;

import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.view.ReChargePayView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/10/8.
 */
public class ReChargePayPresenter extends BasePresenter<ReChargePayView> {
    /**
     * @param type   1,支付宝2,微信
     * @param money  金额
     * @param mcType 类型  1 充值  2 VIP
     */
    public void pay(int type, String money, int mcType) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("money", money);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        if (mcType == 1) {
            addTask(RetrofitHelper.getInstance().getService().reChargePay(requestBody), respond -> {
                getView().hideLoadingView();
                HttpRespond<String> httpResult = Global.getHttpResult(respond);
                if (httpResult.result == 1) {
                    getView().showPayRes(httpResult.data);
                } else
                    getView().onRequestFailed(httpResult.message);
            });
        }else {
            addTask(RetrofitHelper.getInstance().getService().vipPay(requestBody), respond -> {
                getView().hideLoadingView();
                HttpRespond<String> httpResult = Global.getHttpResult(respond);
                if (httpResult.result == 1) {
                    getView().showPayRes(httpResult.data);
                } else
                    getView().onRequestFailed(httpResult.message);
            });
        }
    }
}
